home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1992 by Panagiotis Tsirigotis
- * All rights reserved. The file named COPYRIGHT specifies the terms
- * and conditions for redistribution.
- */
-
- static char RCSid[] = "$Id: slog.c,v 1.5 1993/01/08 01:37:38 panos Exp $" ;
-
- #include <syslog.h>
- #include <varargs.h>
-
- #include "xlog.h"
- #include "impl.h"
- #include "slog.h"
-
- #define MSGBUFSIZE 2048
-
-
- PRIVATE int syslog_init() ;
- PRIVATE void syslog_fini() ;
- PRIVATE int syslog_control() ;
- PRIVATE int syslog_write() ;
- PRIVATE int syslog_parms() ;
-
- static struct syslog_parms parms =
- {
- 0,
- LOG_PID + LOG_NOWAIT,
- LOG_USER,
- "XLOG",
- FALSE
- } ;
-
-
- struct xlog_ops __xlog_syslog_ops =
- {
- syslog_init,
- syslog_fini,
- syslog_write,
- syslog_control,
- syslog_parms
- } ;
-
- char *malloc() ;
- void closelog() ;
-
- /*
- * Expected arguments:
- * facility, level
- */
- PRIVATE int syslog_init( xp, ap )
- xlog_s *xp ;
- va_list ap ;
- {
- register struct syslog_parms *sp = &parms ;
- struct syslog *slp ;
-
- slp = NEW( struct syslog ) ;
- if ( slp == NULL )
- return( XLOG_ENOMEM ) ;
-
- slp->facility = va_arg( ap, int ) ;
- slp->default_level = va_arg( ap, int ) ;
- if ( sp->n_xlogs++ == 0 )
- openlog( sp->ident, sp->logopts, sp->facility ) ;
- xp->data = slp ;
- return( XLOG_ENOERROR ) ;
- }
-
-
- PRIVATE void syslog_fini( xp )
- xlog_s *xp ;
- {
- if ( --parms.n_xlogs == 0 )
- closelog() ;
- FREE( SYSLOG( xp ) ) ;
- xp->data = NULL ;
- }
-
-
- PRIVATE int syslog_control( xp, cmd, ap )
- xlog_s *xp ;
- xlog_cmd_e cmd ;
- va_list ap ;
- {
- switch ( cmd )
- {
- case XLOG_LEVEL:
- SYSLOG( xp )->default_level = va_arg( ap, int ) ;
- break ;
-
- case XLOG_FACILITY:
- SYSLOG( xp )->facility = va_arg( ap, int ) ;
- break ;
-
- case XLOG_PREEXEC:
- closelog() ;
- break ;
-
- case XLOG_POSTEXEC:
- if ( parms.n_xlogs )
- openlog( parms.ident, parms.logopts, parms.facility ) ;
- break ;
- }
- return( XLOG_ENOERROR ) ;
- }
-
-
- PRIVATE int syslog_write( xp, buf, len, flags, ap )
- xlog_s *xp ;
- char buf[] ;
- int len ;
- int flags ;
- va_list ap ;
- {
- int level ;
- int syslog_arg ;
- char prefix[ MSGBUFSIZE ] ;
- int prefix_size = sizeof( prefix ) ;
- int prefix_len = 0 ;
- int cc ;
- char *percent_m_pos ;
- int action_flags = ( flags | xp->flags ) ;
-
- if ( flags & XLOG_SET_LEVEL )
- level = va_arg( ap, int ) ;
- else
- level = SYSLOG( xp )->default_level ;
- syslog_arg = SYSLOG( xp )->facility + level ;
-
- if ( action_flags & XLOG_PRINT_ID )
- {
- cc = strx_nprint( &prefix[ prefix_len ], prefix_size, "%s: ", xp->id ) ;
- prefix_len += cc ;
- prefix_size -= cc ;
- }
-
- if ( ( action_flags & XLOG_NO_ERRNO ) ||
- ( percent_m_pos = __xlog_add_errno( buf, len ) ) == NULL )
- syslog( syslog_arg, "%.*s%.*s", prefix_len, prefix, len, buf ) ;
- else
- {
- int cc_before_errno = percent_m_pos - buf ;
- int cc_after_errno = len - cc_before_errno - 2 ;
- char *ep ;
- char errno_buf[ 100 ] ;
- unsigned size = sizeof( errno_buf ) ;
-
- ep = __xlog_explain_errno( errno_buf, &size ) ;
- syslog( syslog_arg, "%.*s%.*s%.*s%.*s",
- prefix_len, prefix,
- cc_before_errno, buf,
- (int)size, ep,
- cc_after_errno, &percent_m_pos[ 2 ] ) ;
- }
- return( XLOG_ENOERROR ) ;
- }
-
-
- PRIVATE int syslog_parms( ap )
- va_list ap ;
- {
- char *__xlog_new_string() ;
- char *id = __xlog_new_string( va_arg( ap, char * ) ) ;
-
- if ( id == NULL )
- return( XLOG_ENOMEM ) ;
- parms.ident = id ;
- parms.logopts = va_arg( ap, int ) ;
- parms.facility = va_arg( ap, int ) ;
- return( XLOG_ENOERROR ) ;
- }
-
-